﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_math
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleKeyInfo input_mode;
            
            do{
                fnPrintMode();
                input_mode = Console.ReadKey();
                Console.WriteLine("\n");

                switch (input_mode.Key)
                {
                    case ConsoleKey.D1:
                        fnRoundMode();
                        break;
                    case ConsoleKey.D2:
                        fnRandomMode();
                        break;
                }
            }while(input_mode.Key != ConsoleKey.Q);
        }

        static void fnPrintMode()
        {
            Console.WriteLine("1. Round");
            Console.WriteLine("2. Random");
            Console.WriteLine("q, quit");
        }

        static void fnRoundMode()
        {
            Console.Write("input num: ");
            string input_num = Console.ReadLine();

            Console.Write("input decimals: ");
            string input_decimals = Console.ReadLine();

            double round;

            if (input_decimals.Trim().Equals(""))
            {
                round = fnRound(Convert.ToDouble(input_num));
            }
            else
            {
                round = fnRound(Convert.ToDouble(input_num), Convert.ToInt32(input_decimals));
            }

            Console.WriteLine(round);
        }

        static void fnRandomMode()
        {
            Console.Write("input min: ");
            string input_min = Console.ReadLine();

            Console.Write("input max: ");
            string input_max = Console.ReadLine();

            int random = fnRandom(Convert.ToInt32(input_min), Convert.ToInt32(input_max));

            Console.WriteLine(random);
        }

        static double fnRound(double num)
        {
            double ret;

            ret = Math.Round(num);

            return ret;
        }

        static double fnRound(double num, int decimals)
        {
            double ret;

            ret = Math.Round(num, decimals, MidpointRounding.ToEven);

            return ret;
        }

        static int fnRandom(int min, int max)
        {
            int rnt;

            Random random = new Random();
            rnt = random.Next(min, max); // max -1 까지

            return rnt;
        }
    }
}
